<?php
/**
 * @author zhaofei
 * @package 基础业务控制器 
 * @createtime 2014-11-28
 * @updatetime 2014-11-29
 * @ps:报单信息
 */
namespace System\Controller;
use Think\Controller;
class StaffController extends Controller {
    private $is_send_email;
    private $email;
	function __construct(){
		parent::__construct();
		$ranks = session('ranks');
		if($ranks!=4){
			$this->error('对不起你没有权限',U('Login/index'));
		}
	}

    public function isSend(){
        $rs = D('Config')->field('is_notify_kefu,kefu_email')->find(1);
        if($rs['is_notify_kefu']==1){
            $this->is_send_email = true;
            $this->email = $rs['kefu_email'];
        }else{
            $this->is_send_email = false;
        }        
    }

    // private function getStoreInfo(){
    //     $cons = array('e_id'=>session('id')); //只能选择正常状态的终端
    //     $stores = D('EmStore')->where($cons)->find();
    //     if($stores){
    //         $string='';
    //         foreach ($stores as $key => $value) {
    //             if($key=='store_ids'){
    //                $ids = explode(',', trim($value,',')); 
    //                foreach ($ids as $k => $v) {
    //                    $string.=D('Store')->getStoreBaseById($v)."<br/>";
    //                }
    //                $stores['store'] = $string;
    //             }   
    //         }
    //         return $stores;
    //     }else{
    //         return false;
    //     }
    // }

    public function index(){
    	$conditions = array('id'=>session('id'));
    	$data = D('Employee')->where($conditions)->find();
    	$this->assign('data',$data);
        

    	$this->display();
    }

    //修改密码
    public function password(){
        if(I('post.password')){
            $pass = md5(md5(I('post.password'),true));
            $result = D('Employee')->where(array('id'=>session('id')))->save(array('password'=>$pass));
            if($result){
                $this->success('修改成功，当前用户自动注销',U('Login/logout'));
            }else{
                $this->error('修改失败');
            }
        }else{
            $this->error('密码不能为空');
        }
    }
    //新增报单
    public function add(){
        // $stores = $this->getStoreInfo();
        // if($stores['store_ids']){
        //     $this->assign('store_ids',$stores['store_ids']);
        // }else{
        //     $this->error('你还没有关联终端');
        // }
        //products
        // $data = D('Products_class')->order('sort asc')->where(array('status'=>1))->select();
        // $num = count($data);
        // for($i=0;$i<$num;$i++) {
        //     $del = D('Products_class')->where(array('p_id'=>$data[$i]['id']))->find();
        //     if($del){
        //         unset($data[$i]);
        //     }
        // }
        // $this->assign('data',$data);
        $this->display();
    }


    // public function getProductList(){
    //     $p_id = I('post.p_id');
    //     $data = array();
    //     if($p_id){
    //         $products = D('Products')->where(array('p_id'=>$p_id))->
    //             order('createtime desc')->select();
    //         if($products){
    //             $data = array('status'=>1,'data'=>$products);
    //         }else{
    //             $data = array('status'=>0,'data'=>'该类下无产品');
    //         }
    //     }else{
    //         $data = array('status'=>0,'data'=>'参数错误');
    //     }
    //     $this->ajaxReturn($data);
    // }

    public function insertOrder(){
        $data = D('Order')->create();
        if($data){
            $rs = D('Order')->add($data);
            if($rs){
                /*是否发送邮件*/
                $this->isSend();
                if($this->is_send_email){
                    $href = 'http://'.$_SERVER['HTTP_HOST'];
                    $body = $data['content']."<a href=$href>详情请登陆OA</a>";
                    $msg = send_email($this->email,$data['title'],$body,true);
                    if($msg){
                        $mail_records = array(
                            'from'=>C('Mail_From'),
                            'to'=>$this->email,
                            'title'=>$data['title'],
                            'body'=>$data['content'],
                            'time'=>time(),
                            );
                        D('Email')->add($mail_records);
                        $info = '通知成功';
                    }
                }
                $this->success('报单成功'.$info,U('order'));
            }else{
                // dump($data);
                $this->error('报单失败');
            }
        }else{
            $this->error(D('Order')->getError());
        }
    }


    public function order(){
        $cons = array('e_id'=>session('id'));
        if(isset($_GET['st'])){
            $cons['examize_status'] = $_GET['st'] ; 
        }
        if(isset($_GET['month'])){
            $month_s = date('Y-m');
            $cons['write_time'] = array('between',array(strtotime($month_s),time()));
        }
        $model = M('Order'); // 实例化
        $count = $model->where($cons)->count();// 查询满足要求的总记录数
          // echo $model->getLastSql();
        // exit();
        $Page = new \Think\Page($count,5);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $show = $Page->show();// 分页显示输出
        // 进行分页数据查询 注意limit方法的参数要使用Page类的属性
        $list = $model->where($cons)->order('write_time desc')->limit($Page->firstRow.','.$Page->listRows)->select();
        $this->assign('list',$list);// 赋值数据集
        $this->assign('page',$show);// 赋值分页输出
        $this->display(); // 输出模板
    }

    public function edit($id){
        $data = D('Order')->where(array('id'=>$id))->find();
        if($data['examize_status']!=0){
            $this->error('当前报单更改期已过');
        }
        if(I('post.')){
            $value = D('Order')->create();
            if($value){
                $rs = D('Order')->where(array('id'=>$id))->save($value);
                 if($rs){
                    $this->success('修改成功',U('order'));
                }else{
                    $this->error('修改失败');
                }
            }else{
                $this->error(D('Order')->getError());
            }
        }else{

            $this->assign('data',$data);
            $this->display('edit');
        }

    }


    public function view($id){
        $data = D('Order')->where(array('id'=>$id))->find();
        // dump($data);
        $this->assign('data',$data);
        $this->display();  
    }
    
    



}
